package com.augurit.gzcgwsp.jzfqwczz.common.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoHfqkService;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnBusinfoHfqkForm;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnBusinfoService;
import com.augurit.gzcgwsp.ywcmn.entity.YwComQyxx;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "yw-yn-businfo!hfqkList.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/gzcgwsp/ywcmn/YwComClxx/YwComClxx.jsp"),
	@Result(name ="toAllhfqk", location = "/gzcgwsp/ywcmn/YwComClxx/toAllhfqk.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/gzcgwsp/ywcmn/YwComClxx/YwComClxx-input.jsp"),
	@Result(name = "hfqkList", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-hfqkList.jsp"),
	@Result(name = "hfqkEdit", location = "/gzcgwsp/ywcmn/YwComClxx/YwYnBusinfo-hfqkEdit.jsp"),
	@Result(name = "kspList", location = "yw-yn-businfo!kspList.action", type = "redirect"),
	@Result(name = "fgcspList", location = "yw-yn-businfo!fgcspList.action", type = "redirect"),
	@Result(name = "cspList", location = "yw-yn-businfo!cspList.action", type = "redirect")
})
public class YwYnBusinfoHfqkAction extends CrudActionSupport<YwYnBusinfoHfqkForm>{
	@Autowired
	private IYwYnBusinfoService ywYnBusinfoService;
	@Autowired
	private IYwYnBusinfoHfqkService ywYnBusinfoHfqkService;
    @Autowired
    private ISysCodeService syscodeService;

	private YwYnBusinfoHfqkForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<YwYnBusinfoHfqkForm> page = new Page<YwYnBusinfoHfqkForm>(10);
	private String params;
	
	
	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		ywYnBusinfoHfqkService.search(page, form);
		return SUCCESS;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		
		return null;
	}
	
	/**
	 * 到全部核发的页面
	 */
	public String toAllHfqkInfo() throws Exception {
		String ids=getParameter("ids");
		Date date=DateUtilsEX.getDateByMonth( new Date(), 6,true);
		getRequest().setAttribute("startdate", DateUtilsEX.dateFormate(new Date(),"yyyy-MM-dd"));
		getRequest().setAttribute("checkdate", DateUtilsEX.dateFormate(new Date(),"yyyy-MM-dd"));
		getRequest().setAttribute("enddate", DateUtilsEX.dateFormate(date,"yyyy-MM-dd"));
		getRequest().setAttribute("id", ids);
		return "toAllhfqk";
	}

	//全部核发
	public String  saveAllHfqk() throws Exception {
		String flag=getRequest().getParameter("flag");
		String ids=getRequest().getParameter("ids");
		String checkdate=getRequest().getParameter("checkdate");
		String startdate=getRequest().getParameter("startdate");
		String enddate=getRequest().getParameter("enddate");
		String curUser=this.getLoginUser().getUser().getUserName();
		
		ywYnBusinfoHfqkService.saveAllHfqk(ids,flag,curUser,checkdate,startdate,enddate);
		return null;
	}
	
	/**
	 * 根据行驶证日期计算有效时间
	 * @return
	 * @throws Exception
	 */
	public String setDate() throws Exception {
		String xszyxq=getRequest().getParameter("xszyxqDate");
		String yxksrq=getRequest().getParameter("yxksrq");
		String busId=getRequest().getParameter("busId");
		if(StringUtils.isNotBlank(xszyxq) && StringUtils.isNotBlank(yxksrq)){
			Date startDate=DateUtilsEX.dateFormate(yxksrq, "yyyy-MM-dd");
			Date xszyxqDate=DateUtilsEX.dateFormate(xszyxq, "yyyy-MM");
			Calendar calendar = Calendar.getInstance();      
	    	calendar.setTime(xszyxqDate);   
    		int maxDay=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
	    	calendar.set(Calendar.DAY_OF_MONTH, maxDay);
	    	xszyxqDate=calendar.getTime();//到当月的最后一天
			//6个月计算时间
			Date endDate=DateUtilsEX.getDateByMonth(startDate, 6,true);
			//处置证有效期
			Date validEndtime=this.ywYnBusinfoHfqkService.getCzzDate(busId);
			if(validEndtime.getTime()>=xszyxqDate.getTime()){//处置证有效期要大于行驶证有效期
				if(xszyxqDate.getTime()>=endDate.getTime() && validEndtime.getTime()>=endDate.getTime()){
					getResponse().getWriter().print(DateUtilsEX.dateTimeToString(endDate, "yyyy-MM-dd"));
				}else{
					getResponse().getWriter().print(DateUtilsEX.dateTimeToString(xszyxqDate, "yyyy-MM-dd"));
				}
			}else{
				if(validEndtime.getTime()>=endDate.getTime()){
					getResponse().getWriter().print(DateUtilsEX.dateTimeToString(endDate, "yyyy-MM-dd"));
				}else{
					getResponse().getWriter().print(DateUtilsEX.dateTimeToString(validEndtime, "yyyy-MM-dd"));
				}
			}
			
		}
		return null;
	}

	
	
	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save()  {
		String flag =getParameter("flag");
		ywYnBusinfoHfqkService.save(form,flag);
		if("ksp".equals(flag)){
			return "kspList";
		}else if("fgcsp".equals(flag)){
			return "fgcspList";
		}else if("csp".equals(flag)){
			return "cspList";
		}
		return RELOAD;
	}
	
	/**
	 * 年审核发（后台）
	 * @return
	 * @throws Exception
	 */
	public String nshfList() throws Exception {
		ywYnBusinfoHfqkService.search(page, form);
		return "hfqkList";
	}
	
	/**
	 * 修改审核结果
	 * @return
	 * @throws Exception
	 */
	/*public String edit()  {
		String busId= this.getRequest().getParameter("busId");
		form.setBusId(Long.parseLong(busId));
		List<YwYnBusinfoHfqkForm> businfoHfqkList = ywYnBusinfoHfqkService.search(page, form).getResult();
		if(businfoHfqkList != null && businfoHfqkList.size() > 0){
			form = businfoHfqkList.get(0);
			this.getRequest().setAttribute("form", form);
		}
		return "hfqkEdit";
	}*/
	
	
	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		if(id != null){
			ywYnBusinfoHfqkService.delete(id);
			return RELOAD;
		}
		return null;
	}
	
	/**
	 * 删除多条记录
	 */
	@Override
	public String deleteMore() throws Exception{
		ywYnBusinfoHfqkService.delete(checkedIds);
		return RELOAD;
	}
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
	
		if(id != null && id.intValue()!=0){
			form = ywYnBusinfoHfqkService.get(id);
		}
		else
			form = new YwYnBusinfoHfqkForm();
	}
	

	public YwYnBusinfoHfqkForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<YwYnBusinfoHfqkForm> getPage() {
		return page;
	}


	public void setPage(Page<YwYnBusinfoHfqkForm> page) {
		this.page = page;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}
	
	public YwYnBusinfoHfqkForm getForm() {
		return form;
	}

	public void setForm(YwYnBusinfoHfqkForm form) {
		this.form = form;
	}

}
